package com.nuanshui.heatedloan.credit.service.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.lang.DateUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.constant.OrderType;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.facade.job.PayOrderJobService;
import com.nuanshui.heatedloan.credit.request.OrderJobParams;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import com.nuanshui.heatedloan.task.facade.QuartzTaskManager;
import com.nuanshui.heatedloan.task.facade.bean.JobParams;
import com.nuanshui.heatedloan.task.facade.bean.JobProvider;
import com.nuanshui.heatedloan.task.facade.constant.TaskEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by wangxw on 2017/7/25.
 */

@Service(ServiceConstant.PAYORDERJOBSERVICE)
public class PayOrderJobServiceImpl implements PayOrderJobService {


    private Logger logger = LoggerFactory.getLogger(PayOrderJobServiceImpl.class);

    @Reference(url= GlobalParam.TASK_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.TASK_DUBBO_SERVER_VERSION)
    private QuartzTaskManager quartzTaskManager;

    @Autowired
    private RabbitmqMessageSender rabbitmqMessageSender;

    @Autowired
    private PayOrderInfoService payOrderInfoServic;

    @Override
    public void createExpireRefundJob(OrderJobParams orderJobParams) {
        logger.info("===========开始生成定时任务,定时任务的名称和组为"+
                orderJobParams.getTaskType().getName(),orderJobParams.getTaskType().getName()+"===========");
        logger.info("===========开始生成定时任务,订单号为"+orderJobParams.getOrderNo()+"===============");
        JobProvider jobProvider=new JobProvider(orderJobParams.getTaskType().getName(),orderJobParams.getTaskType().getGroup());
        jobProvider.getParams().setBizKey(orderJobParams.getOrderNo());
        jobProvider.addParams(JobParams.ORDER_NUM, orderJobParams.getOrderNo());
        Date jobDate=orderJobParams.getOrderExpireTime();
        quartzTaskManager.addJob(jobProvider,jobDate);
        LogUtil.info(logger, String.format("----------测试, 使用配置的失效时间执行, %s----------", DateUtil.format(jobDate, "yyyyMMdd-HH:mm:ss")));
        }


    public void paySuccessful(String orderNo){
        PayOrderInfoVO  payOrderInfoVO=payOrderInfoServic.loadVOByOrderNo(orderNo);
        Date returnDate=payOrderInfoVO.getShouldReturnTime();
        long dayNum=DateUtil.getIntervalDay(new Date(),returnDate);   //还款当天的话剩余是0 该值已经取了绝对值
        //根据状态来看是逾期前还是逾期后状态
        int orderStatus=payOrderInfoVO.getOrderStatus();
        //逾期的
        if(OrderType.OVERDUEPAYMENT==orderStatus){
            JobProvider jobProvider=new JobProvider();
            jobProvider.setJobGroup(TaskEnum.CALCULATE_LATE_PAYMENT_JOB.getGroup());
            jobProvider.setJobName(TaskEnum.CALCULATE_LATE_PAYMENT_JOB.getName()+orderNo);
            quartzTaskManager.stopJob(jobProvider);
            //取消两个定时任务
            if(dayNum<=10){
                JobProvider jobRemindProvider=new JobProvider();
                jobRemindProvider.setJobGroup(TaskEnum.LATE_REMIND_JOB.getGroup());
                jobRemindProvider.setJobName(TaskEnum.LATE_REMIND_JOB.getName()+orderNo);
                quartzTaskManager.stopJob(jobRemindProvider);
            }
        }
        //没有逾期的
        else{
            //取消两个定时任务
            JobProvider jobExpireProvider=new JobProvider();
            jobExpireProvider.setJobGroup(TaskEnum.REFUND_EXPIRERE_JOB.getGroup());
            jobExpireProvider.setJobName(TaskEnum.REFUND_EXPIRERE_JOB.getName()+orderNo);
            logger.info("=========="+TaskEnum.REFUND_EXPIRERE_JOB.getName()+orderNo+"=========");
            quartzTaskManager.stopJob(jobExpireProvider);
            JobProvider jobReminderRefundJob=new JobProvider();
            jobReminderRefundJob.setJobGroup(TaskEnum.REMINDER_REFUND_JOB.getGroup());
            jobReminderRefundJob.setJobName(TaskEnum.REMINDER_REFUND_JOB.getName()+orderNo);
            logger.info("=========="+TaskEnum.REMINDER_REFUND_JOB.getName()+orderNo+"=========");
            quartzTaskManager.stopJob(jobReminderRefundJob);
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.dateNum(DateUtil.getFirstWeekDay(new Date()),new Date()));
    }
}
